package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.QueryPageBean;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 检查组的控制器
 */
@RestController("/CheckGroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService cs;

    @PostMapping("/CheckGroup/{checkItemIds}")
    public Result add(@RequestBody CheckGroup checkGroup, @PathVariable int[] checkItemIds) {
        System.out.println("拿到的数据是");
        System.out.println("checkGroup = " + checkGroup);
        System.out.println("checkItemIds = " + checkItemIds);

        int row = cs.add(checkGroup, checkItemIds);
        Result result = null;
        if (row > 0) {
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } else {
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return result;
    }

    @GetMapping("/CheckGroup")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult<CheckGroup> pageResult = cs.findPage(queryPageBean);

            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
